﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Web.Caching;

namespace DataServer
{
    /// <summary>
    /// 在指定的文件夹下读取终端信息
    /// </summary>
    public class ReadClientFile
    {
        //将数据展示在ListView
        private ListView viewList;
        public ListView ViewList
        {
            get { return viewList; }
            set { viewList = value; }
        }
        private string _filePath = System.Configuration.ConfigurationManager.AppSettings["ReadFilePath"];
        private string _readFileTime = System.Configuration.ConfigurationManager.AppSettings["ReadFileTime"];
        private DataTable GetClientInfoByFile(out OptsModel optsmodel)
        {
            optsmodel = new OptsModel();
            DataTable infoDt = new DataTable();
            infoDt.Columns.Add("cId", System.Type.GetType("System.String"));
            infoDt.Columns.Add("cIp", System.Type.GetType("System.String"));
            infoDt.Columns.Add("cPort", System.Type.GetType("System.String"));
            infoDt.Columns.Add("cEndRecTime", System.Type.GetType("System.String"));
            try
            {
                List<string[]> listInfo = GetInfoByFile();
                DataRow dr;
                foreach (string[] strs in listInfo)
                {
                    dr = infoDt.NewRow();
                    dr["cId"] = strs[0].Trim();
                    dr["cIp"] = strs[1].Trim();
                    dr["cPort"] = strs[2].Trim();
                    dr["cEndRecTime"] = strs[3].Trim();
                    infoDt.Rows.Add(dr);
                }
                optsmodel.Succeed = true;
            }
            catch (Exception e)
            {
                optsmodel.ExceptionMsg = e.Message;
            }
            return infoDt;
        }
        private List<string[]> GetInfoByFile()
        {
            List<string[]> infoList = new List<string[]>();
            FileInfo[] list = new DirectoryInfo(@_filePath).GetFiles();
            Array.Sort<FileInfo>(list, new FIleLastTimeComparer());
            string filePathName = list[list.Length - 1].FullName;//文件夹的全路径以及名字
            StringBuilder b = new StringBuilder();
            using (FileStream fs = new FileStream(filePathName, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    string[] strinfos;
                    while (!sr.EndOfStream)
                    {
                        string sLine = sr.ReadLine();
                        if (sLine.Length < 1)
                        {
                            continue;
                        }
                        ///     0/117.135.134.126/  9999/   2012-8-2 16:12:27
                        strinfos = sLine.Split('/');
                        if (strinfos.Length == 4)
                        {
                            infoList.Add(strinfos);
                        }
                    }
                }
            }
            return infoList;
        }
        //文件夹中按时间排序最新的文件读取  
        private class FIleLastTimeComparer : IComparer<FileInfo>
        {
            #region IComparer<FileInfo> 成员

            public int Compare(FileInfo x, FileInfo y)
            {
                return x.LastWriteTime.CompareTo(y.LastWriteTime);
            }

            #endregion
        }
        //文件夹中按时间排序最新的文件夹读取  
        private class DirectoryLastTimeComparer : IComparer<DirectoryInfo>
        {
            #region IComparer<DirectoryInfo> 成员

            public int Compare(DirectoryInfo x, DirectoryInfo y)
            {
                return x.LastWriteTime.CompareTo(y.LastWriteTime);

            }
            #endregion
        }
        public void SetListViewData(DataTable dt)
        {
            if (ViewList != null)
            {
                ListViewItem lstItem;
                int rowindex = 1;
                foreach (DataRow row in dt.Rows)
                {
                    lstItem = new ListViewItem();
                    lstItem.SubItems[0].Text = rowindex + "";
                    lstItem.SubItems.Add(row["cId"].ToString().Trim());
                    lstItem.SubItems.Add(row["cIp"].ToString().Trim());
                    lstItem.SubItems.Add(row["cPort"].ToString().Trim());
                    lstItem.SubItems.Add(row["cEndRecTime"].ToString().Trim());
                    ViewList.Items.Add(lstItem);
                    rowindex++;
                }
            }
        }
        /// <summary>
        /// 加载客户端信息
        /// </summary>
        public OptsModel LoadClientInfo()
        {
            OptsModel optsM = new OptsModel();
            DataTable cDt = GetClientInfoByFile(out optsM);
            if (optsM.Succeed)
            {
                Program.Cache.Insert("ClientDatable", cDt, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
                //将终端信息加载到ListView中
                SetListViewData(cDt);
            }
            return optsM;
        }
    }
}
